VFG.EditRoadMark=function(viewer,entity){
	this.viewer=viewer;
	this.tip=new VFG.Tip(viewer);
	this.entity=entity;
	this.position=entity.position||null;
	this.model=entity;
	
	this.init();
}
VFG.EditRoadMark.prototype.init=function(){
	var _this=this;
	
	_this.rectMin=new Cesium.Cartesian3 (this.entity.thing.coordinates[0].x , this.entity.thing.coordinates[0].y ,this.entity.thing.coordinates[0].z );  
	_this.rectMax=new Cesium.Cartesian3 (this.entity.thing.coordinates[1].x , this.entity.thing.coordinates[1].y ,this.entity.thing.coordinates[1].z );  
	
	_this.rectMinPoint=_this.viewer.entities.add({
		id:_this.entity.id+'-rectMin',
        position: new Cesium.CallbackProperty(function (time) {
            return _this.rectMin;
        }, false),
        point:{
            color: Cesium.Color.VIOLET  ,
            pixelSize:10,
        }
    });
	
	_this.rectMaxPoint=_this.viewer.entities.add({
		id:_this.entity.id+'-rectMax',
        position: new Cesium.CallbackProperty(function (time) {
            return _this.rectMax;
        }, false),
        point:{
            color: Cesium.Color.VIOLET  ,
            pixelSize:10,
        }
    });
	
	var rectangle = Cesium.Rectangle.fromCartesianArray(_this.entity.thing.coordinates);
    var center = Cesium.Rectangle.center(rectangle);
    _this.rectCenter= Cesium.Cartographic.toCartesian(center);
    
	_this.rectCenterPoint=_this.viewer.entities.add({
		id:_this.entity.id+'-rectCenter',
        position: new Cesium.CallbackProperty(function (time) {
            return  _this.rectCenter;
        }, false),
        point:{
            color: Cesium.Color.VIOLET  ,
            pixelSize:10,
        }
    });
	
	this._handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
	
	//鼠标左键按下
	this._handler.setInputAction(function(e) {
		let pick = _this.viewer.scene.pick(e.position);
		if(Cesium.defined(pick) && Cesium.defined(pick.id)){
			if(pick.id.id==_this.entity.id+'-rectMax' ){
				_this.pickEntity=pick;
				_this.rectMaxPoint.point.color=Cesium.Color.YELLOW;
		 		_this.viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
			}
			if(pick.id.id==_this.entity.id+'-rectMin' ){
				_this.pickEntity=pick;
				_this.rectMinPoint.point.color=Cesium.Color.YELLOW;
		 		_this.viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
			}
			if(pick.id.id==_this.entity.id+'-rectCenter' ){
				_this.pickEntity=pick;
				_this.rectCenterPoint.point.color=Cesium.Color.YELLOW;
		 		_this.viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
			}
		}
	},Cesium.ScreenSpaceEventType.LEFT_DOWN);
    
	this._handler.setInputAction(function(e) {
		if(_this.pickEntity){
			if(_this.pickEntity.id.id==_this.entity.id+'-rectMax' ){
				_this.rectMaxPoint.point.color=Cesium.Color.VIOLET;
			}
			if(_this.pickEntity.id.id==_this.entity.id+'-rectMin' ){
				_this.rectMinPoint.point.color=Cesium.Color.VIOLET;
			}
			if(_this.pickEntity.id.id==_this.entity.id+'-rectCenter' ){
				_this.rectCenterPoint.point.color=Cesium.Color.VIOLET;
			}
		}
		_this.viewer.scene.screenSpaceCameraController.enableRotate = true;
		_this.tip.setVisible(false);
		_this.pickEntity=null;
	},Cesium.ScreenSpaceEventType.LEFT_UP);
	
	this._handler.setInputAction(function(movement) {
		let pick = _this.viewer.scene.pick(movement.endPosition);
		if(Cesium.defined(pick) && Cesium.defined(pick.id) && _this.pickEntity==null){
			if(pick.id.id==_this.entity.id+'-rectCenter' ||pick.id.id==_this.entity.id+'-rectMax' || pick.id.id==_this.entity.id+'-rectMin'){
				_this.tip.setVisible(true);
				_this.tip.showAt(movement.endPosition,"按住鼠标左键可拖动位置。");
			}else{
				_this.tip.setVisible(false);
			}
		}else{
			_this.tip.setVisible(false);
		}
		
		if(_this.pickEntity && Cesium.defined(_this.pickEntity.id)){
			if(_this.pickEntity.id.id==_this.entity.id+'-rectCenter'){
				let startC3=VFG.Util.getScreenToC3(_this.viewer, movement.startPosition, _this.pickEntity);
				let endC3=VFG.Util.getScreenToC3(_this.viewer, movement.endPosition, _this.pickEntity);
				if(Cesium.defined(startC3) && Cesium.defined(endC3)){
					_this.move(startC3,endC3);
					_this.tip.setVisible(true);
					_this.tip.showAt(movement.endPosition,"调整中...");
				}
			}
			if(_this.pickEntity.id.id==_this.entity.id+'-rectMin'){
				let endC3=VFG.Util.getScreenToC3(_this.viewer, movement.endPosition, _this.pickEntity);
				if(Cesium.defined(endC3)){
					_this.moveMin(endC3);
					_this.tip.setVisible(true);
					_this.tip.showAt(movement.endPosition,"调整中...");
				}
			}
			if(_this.pickEntity.id.id==_this.entity.id+'-rectMax'){
				let endC3=VFG.Util.getScreenToC3(_this.viewer, movement.endPosition, _this.pickEntity);
				if(Cesium.defined(endC3)){
					_this.moveMax(endC3);
					_this.tip.setVisible(true);
					_this.tip.showAt(movement.endPosition,"调整中...");
				}
			}
		}
	}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	
	this._handler.setInputAction(function(movement) {
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}


VFG.EditRoadMark.prototype.on = function(type,callback) {
	if('change'==type){
		this.change=callback;
	}
};

VFG.EditRoadMark.prototype.move = function(oldPosition,newPosition) {
	let _this=this;
	let diff = Cesium.Cartesian3.subtract(newPosition, oldPosition, new Cesium.Cartesian3());
	_this.rectMin=Cesium.Cartesian3.add(_this.rectMin, diff, new Cesium.Cartesian3());
	_this.rectMax=Cesium.Cartesian3.add(_this.rectMax, diff, new Cesium.Cartesian3());
    _this.rectCenter= Cesium.Cartographic.toCartesian(Cesium.Rectangle.center(Cesium.Rectangle.fromCartesianArray([_this.rectMin,_this.rectMax])));
    if(this.entity){
    	this.entity.updateCoordinates([_this.rectMin,_this.rectMax],true);
    }
};

VFG.EditRoadMark.prototype.moveMin = function(minPosition) {
	let _this=this;
	_this.rectMin=minPosition;
    _this.rectCenter= Cesium.Cartographic.toCartesian(Cesium.Rectangle.center(Cesium.Rectangle.fromCartesianArray([_this.rectMin,_this.rectMax])));
    if(this.entity){
    	this.entity.updateCoordinates([_this.rectMin,_this.rectMax],true);
    }
};

VFG.EditRoadMark.prototype.moveMax = function(maxPosition) {
	let _this=this;
	_this.rectMax=maxPosition;
    _this.rectCenter= Cesium.Cartographic.toCartesian(Cesium.Rectangle.center(Cesium.Rectangle.fromCartesianArray([_this.rectMin,_this.rectMax])));
    if(this.entity){
    	this.entity.updateCoordinates([_this.rectMin,_this.rectMax],true);
    }
};

VFG.EditRoadMark.prototype.destroy = function() {
    if( this._handler){
    	this._handler.destroy();
    	this._handler = null;
    };
    if(this.tip){
    	this.tip.destroy();
    }
    if(this.rectMaxPoint){
    	this.viewer.entities.remove(this.rectMaxPoint);
    }
    if(this.rectMinPoint){
    	this.viewer.entities.remove(this.rectMinPoint);
    }
    if(this.rectCenterPoint){
    	this.viewer.entities.remove(this.rectCenterPoint);
    }
    delete this.rectMaxPoint;
    delete this.rectMinPoint;
    delete this.rectCenterPoint;
    delete this.tip;
    delete this._handler;
	return Cesium.destroyObject(this);
};
